<html>
<head><meta charset="utf-8"><title>create ptr to elem in unaligned array · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create.20ptr.20to.20elem.20in.20unaligned.20array.html">create ptr to elem in unaligned array</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="166391584"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create%20ptr%20to%20elem%20in%20unaligned%20array/near/166391584" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create.20ptr.20to.20elem.20in.20unaligned.20array.html#166391584">(May 23 2019 at 19:26)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> </p>
<blockquote>
<p>Given <code>x: *const [T]</code>, how would I write the equivalent of <code>&amp;raw mut (*x)[i]</code>? </p>
</blockquote>
<p>How would you do that with <code>raw</code> ? AFAICT, the array itself <code>[T]</code> can be at an unaligned memory location (that is the <code>len</code> and the <code>ptr</code> of the <code>[T]</code> are not properly aligned), and <code>(*x)[i]</code> creates a <code>&amp;self</code>, which would be invalid right ?</p>



<a name="166393609"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create%20ptr%20to%20elem%20in%20unaligned%20array/near/166393609" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create.20ptr.20to.20elem.20in.20unaligned.20array.html#166393609">(May 23 2019 at 19:49)</a>:</h4>
<p>why would that create a <code>&amp;self</code>?</p>



<a name="166393819"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create%20ptr%20to%20elem%20in%20unaligned%20array/near/166393819" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create.20ptr.20to.20elem.20in.20unaligned.20array.html#166393819">(May 23 2019 at 19:51)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> because the <code>&lt;[T] as IndexMut&gt;::index_mut(&amp;mut self)</code> method is called, or isn't it?</p>



<a name="166393981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create%20ptr%20to%20elem%20in%20unaligned%20array/near/166393981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create.20ptr.20to.20elem.20in.20unaligned.20array.html#166393981">(May 23 2019 at 19:53)</a>:</h4>
<p>I read <code>&amp;raw mut (*x)[i]</code> as <code>&amp;raw mut &lt;[T] as IndexMut&gt;::index_mut(*x: &amp;mut [T], i)</code></p>



<a name="166394092"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create%20ptr%20to%20elem%20in%20unaligned%20array/near/166394092" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create.20ptr.20to.20elem.20in.20unaligned.20array.html#166394092">(May 23 2019 at 19:54)</a>:</h4>
<p>or am I reading the parenthesis wrong, and that should be <code>&lt;[T] as IndexMut&gt;::index_mut(*x: &amp;raw mut [T], i)</code> ?</p>



<a name="166394124"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create%20ptr%20to%20elem%20in%20unaligned%20array/near/166394124" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create.20ptr.20to.20elem.20in.20unaligned.20array.html#166394124">(May 23 2019 at 19:55)</a>:</h4>
<p>or do we have an <code>IndexRawMut::index_raw_mut(x: &amp;raw mut self, i: usize)</code> API ?</p>



<a name="166394247"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create%20ptr%20to%20elem%20in%20unaligned%20array/near/166394247" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create.20ptr.20to.20elem.20in.20unaligned.20array.html#166394247">(May 23 2019 at 19:56)</a>:</h4>
<p>as in, <code>(&amp;raw mut (*x))[i]</code> expands to <code>&lt;[T] as IndexRawMut&gt;::index_raw_mut(&amp;raw mut (*x), i): &amp;raw mut T</code> ?</p>



<a name="166403471"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create%20ptr%20to%20elem%20in%20unaligned%20array/near/166403471" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create.20ptr.20to.20elem.20in.20unaligned.20array.html#166403471">(May 23 2019 at 21:51)</a>:</h4>
<p>I thought this was a <em>hypothetical</em> <code>IndexRawMut</code> overload of the indexing operator for raw pointer slices</p>



<a name="166431198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create%20ptr%20to%20elem%20in%20unaligned%20array/near/166431198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create.20ptr.20to.20elem.20in.20unaligned.20array.html#166431198">(May 24 2019 at 07:58)</a>:</h4>
<p><code>IndexMut</code> does not get called when indexing a raw slice with a usize</p>



<a name="166431203"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create%20ptr%20to%20elem%20in%20unaligned%20array/near/166431203" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create.20ptr.20to.20elem.20in.20unaligned.20array.html#166431203">(May 24 2019 at 07:58)</a>:</h4>
<p>(and we dont support indexing them with anything else)</p>



<a name="166468197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create%20ptr%20to%20elem%20in%20unaligned%20array/near/166468197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create.20ptr.20to.20elem.20in.20unaligned.20array.html#166468197">(May 24 2019 at 16:28)</a>:</h4>
<p>So what gets called ?</p>



<a name="166468267"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create%20ptr%20to%20elem%20in%20unaligned%20array/near/166468267" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create.20ptr.20to.20elem.20in.20unaligned.20array.html#166468267">(May 24 2019 at 16:29)</a>:</h4>
<p>The only thing I can think of is the _hypothetical_ <code>IndexRawMut</code> trait that <span class="user-mention" data-user-id="124289">@rkruppe</span> mentions.</p>



<a name="166468313"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create%20ptr%20to%20elem%20in%20unaligned%20array/near/166468313" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create.20ptr.20to.20elem.20in.20unaligned.20array.html#166468313">(May 24 2019 at 16:30)</a>:</h4>
<p>IIUC Ralf is saying nothing gets called, it's a compiler built-in</p>



<a name="166468349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create%20ptr%20to%20elem%20in%20unaligned%20array/near/166468349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create.20ptr.20to.20elem.20in.20unaligned.20array.html#166468349">(May 24 2019 at 16:30)</a>:</h4>
<p>That sounds plausible</p>



<a name="166468442"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create%20ptr%20to%20elem%20in%20unaligned%20array/near/166468442" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create.20ptr.20to.20elem.20in.20unaligned.20array.html#166468442">(May 24 2019 at 16:31)</a>:</h4>
<p>So it wouldn't be a <code>#[lang_item]</code> + unstable trait in libcore ? (implementation-wise)</p>



<a name="166468531"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create%20ptr%20to%20elem%20in%20unaligned%20array/near/166468531" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create.20ptr.20to.20elem.20in.20unaligned.20array.html#166468531">(May 24 2019 at 16:32)</a>:</h4>
<p>otherwise it would need to be implemented fairly different than <code>[]</code> overloads for all other types</p>



<a name="166468675"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create%20ptr%20to%20elem%20in%20unaligned%20array/near/166468675" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create.20ptr.20to.20elem.20in.20unaligned.20array.html#166468675">(May 24 2019 at 16:34)</a>:</h4>
<p>This is no different to how <code>impl Add for i32</code> ends up boiling down to just <code>return self + rhs</code>. Injecting built-in operations that precedes the general trait based overloading isn't super difficult.</p>



<a name="166468991"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create%20ptr%20to%20elem%20in%20unaligned%20array/near/166468991" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/create.20ptr.20to.20elem.20in.20unaligned.20array.html#166468991">(May 24 2019 at 16:38)</a>:</h4>
<p>indeed. MIR has a primitive operator to index a slice (works for raw slices and reference slices), just like it has a primitive operator for addition.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>